<template>
  <div class="page-container">
    <div class="card-title" v-show="showSearch">
      <el-form size="small" label-width="68px" :model="queryParams" ref="queryForm">
        <el-row :gutter="48">
          <el-col :md="6" :sm="24">
            <el-form-item label="操作类型">
              <el-select v-model="queryParams.type" placeholder="请选择操作类型" class="search-item" clearable>
                <el-option
                  v-for="dict in dict.type.sys_operate_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="请求方式">
              <el-select v-model="queryParams.requestMethod" placeholder="请选择请求方式" class="search-item" clearable>
                <el-option
                  v-for="dict in dict.type.sys_request_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24">
            <el-form-item label="操作用户">
              <el-select v-model="queryParams.userId" placeholder="请选择操作用户" class="search-item" clearable>
                <el-option
                  v-for="item in userOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <el-form-item label="操作状态">
              <el-select v-model="queryParams.status" placeholder="请选择操作状态" class="search-item" clearable>
                <el-option
                  v-for="dict in dict.type.sys_result_state"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="24" v-if="advanced">
            <el-form-item label="操作时间" size="small">
              <el-date-picker
                v-model="queryParams.dataRange"
                class="search-item"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <div class="search-content">
            <el-form-item class="search-button">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="handleReset">重置</el-button>
              <el-button
                type="text"
                :icon="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
                class="advanced-button"
                @click="toggleAdvanced">
                {{ advanced ? '关闭' : '展开' }}
              </el-button>
            </el-form-item>
          </div>
        </el-row>
      </el-form>
    </div>
    <div class="card">
      <el-row :gutter="10" class="button-container">
        <el-col :span="1.5">
          <el-button type="danger" icon="el-icon-delete" size="mini" plain :disabled="multiple" @click="handleDelete">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" icon="el-icon-folder-opened" size="mini" plain @click="handleClear">清空</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" :columns="columns" @getTableData="getTableData"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" element-loading-text="正在加载表格数据" :data="tableData" @selection-change="handleSelectionChange" border>
        <template slot="empty">
          <el-empty :image-size="200"></el-empty>
        </template>
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column prop="operateContent" label="操作内容" align="center" v-if="columns[1].visible" show-overflow-tooltip></el-table-column>
        <!-- 操作类型 -->
        <el-table-column prop="type" label="操作类型" align="center" v-if="columns[2].visible">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.type === 'ADD'">新增</el-tag>
            <el-tag type="primary" v-else-if="scope.row.type == 'UPDATE'">修改</el-tag>
            <el-tag type="danger" v-else-if="scope.row.type === 'DELETE'">删除</el-tag>
            <el-tag type="warning" v-else-if="scope.row.type === 'QUERY'">查询</el-tag>
            <el-tag type="info" v-else>其他操作</el-tag>
          </template>
        </el-table-column>
        <!-- 操作用户 -->
        <el-table-column prop="nickname" label="操作用户" align="center" v-if="columns[3].visible"></el-table-column>
        <!-- 请求方式 -->
        <el-table-column prop="requestMethod" label="请求方式" align="center" v-if="columns[4].visible">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.requestMethod === 'POST'">POST</el-tag>
            <el-tag type="success" v-else-if="scope.row.requestMethod === 'GET'">GET</el-tag>
            <el-tag type="warning" v-else-if="scope.row.requestMethod === 'PUT'">PUT</el-tag>
            <el-tag type="danger" v-else>DELETE</el-tag>
          </template>
        </el-table-column>
        <!-- 操作IP -->
        <el-table-column prop="operateIp" label="操作IP" align="center" v-if="columns[5].visible"></el-table-column>
        <!-- 操作地址 -->
        <el-table-column prop="operateLocation" label="操作地址" align="center" v-if="columns[6].visible"></el-table-column>
        <!-- 操作状态 -->
        <el-table-column prop="status" label="操作状态" align="center" v-if="columns[7].visible">
          <template slot-scope="scope">
            <div v-if="scope.row.status">
              <div class="dot-success"></div>
              <span>正常</span>
            </div>
            <div v-else>
              <div class="dot-danger"></div>
              <span>失败</span>
            </div>
          </template>
        </el-table-column>
        <!-- 操作消耗时间 -->
        <el-table-column prop="costTime" label="请求耗时" align="center" v-if="columns[8].visible" sortable>
          <template slot-scope="scope">
            <span>{{ scope.row.costTime }}ms</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" align="center" width="180" v-if="columns[9].visible">
          <template slot-scope="scope">
            <span>{{ scope.row.createTime | dateFilter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-view" @click="$refs.viewDrawer.handleView(scope.row)">详细</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <pagination
        :background="false"
        :total="total"
        :page.sync="queryParams.currentPage"
        :limit.sync="queryParams.pageSize"
        @pagination="getTableData"
      />

      <!-- 详细数据抽屉 -->
      <view-drawer ref="viewDrawer" />
    </div>
  </div>
</template>
<script>
import ViewDrawer from './components/ViewDrawer'
import Pagination from '../../../components/Pagination/index'
import RightToolbar from '../../../components/RightToolbar/index'
import { listOperateLogs, deleteOperateLog, clearOperateLogs } from '../../../api/monitor/operate-log'
import { listUserOptions } from '../../../api/user'

export default {
  name: 'OperateLog',

  dicts: ['sys_result_state', 'sys_operate_type', 'sys_request_type'],

  components: { Pagination, RightToolbar, ViewDrawer },

  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 是否展示搜索内容
      showSearch: true,
      // 数据总条数
      total: 0,
      // 数据表格等待框
      loading: false,
      // 查询参数
      queryParams: {
        // 页数
        currentPage: 1,
        // 页面大小
        pageSize: 10,
        // 操作类型
        type: undefined,
        // 请求方式
        requestMethod: undefined,
        // 用户ID
        userId: undefined,
        // 操作状态
        status: undefined,
        // 时间范围
        dataRange: []
      },
      // 对话框按钮等待框
      buttonLoading: false,
      // 列信息
      columns: [
        // 模块ID
        { key: 0, label: '操作模块', visible: true },
        // 操作内容
        { key: 1, label: '操作内容', visible: true },
        // 操作类型
        { key: 2, label: '操作类型', visible: true },
        // 操作用户
        { key: 3, label: '操作用户', visible: true },
        // 请求方式
        { key: 4, label: '请求方式', visible: true },
        // 操作IP
        { key: 5, label: '操作IP', visible: true },
        // 操作地址
        { key: 6, label: '操作地址', visible: true },
        // 操作状态
        { key: 7, label: '操作状态', visible: true },
        // 操作消耗时间
        { key: 8, label: '请求耗时', visible: true },
        { key: 9, label: '操作时间', visible: true }
      ],
      // 表格数据
      tableData: [],
      // 表头选中的数据
      ids: [],
      // 多数据禁用
      multiple: true,
      // 用户选项数据
      userOptions: []
    }
  },

  mounted () {
    // 赋值当前页面内容
    this.title = this.$route.meta.title
    // 查询模块选项
    this.listOptions()
    // 查询表格数据
    this.getTableData()
  },

  methods: {
    // 获取表格数据
    getTableData () {
      // 清除当前模块选中的数据
      this.ids = []
      // 开启加载框
      this.loading = true
      listOperateLogs(this.queryParams).then((res) => {
        // 赋值数据参数
        this.tableData = res.list
        this.total = res.total
        // 关闭等待框
        this.loading = false
      }).catch(() => {
        // 关闭等待框
        this.loading = false
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 查询模块选项数据
    listOptions () {
      listUserOptions().then((res) => {
        this.userOptions = res
      })
    },
    // 点击查询按钮
    handleQuery () {
      this.queryParams.currentPage = 1
      this.getTableData()
    },
    // 点击清空按钮
    handleClear () {
      this.$confirm('此操作将永久删除所有操作日志数据项, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 执行清空操作
        clearOperateLogs().then((res) => {
          this.$message({ type: 'success', message: '清空成功' })
          // 刷新表格
          this.getTableData()
        })
      }).catch(() => {})
    },
    // 点击重置按钮
    handleReset () {
      // 操作类型
      this.queryParams.type = undefined
      // 请求方式
      this.queryParams.requestMethod = undefined
      // 用户ID
      this.queryParams.userId = undefined
      // 操作状态
      this.queryParams.status = undefined
      // 时间范围
      this.queryParams.dataRange = []
    },
    // 点击删除事件
    handleDelete () {
      const operateLogIds = this.ids
      this.$confirm('是否确认删除选中的操作日志数据？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return deleteOperateLog(operateLogIds)
      }).then(() => {
        this.getTableData()
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    // 多选框
    handleSelectionChange (selection) {
      // 获取当前选中数组的ID
      this.ids = selection.map(item => item.id)
      // 表头的删除是否可以点击
      this.multiple = !selection.length
    }
  }
}

</script>

<style lang="scss" scoped>
.dot-success {
  background: #1890ff;
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  position: relative;
  margin-right: 0.5rem;
}
.dot-danger {
  background: red;
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  position: relative;
  margin-right: 0.5rem;
}
</style>
